package com.irun.pm.controller;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;

import com.irun.pm.interfaces.IDriverInfoController;
import com.irun.pm.mode.DriverInfo;
import com.irun.pm.utils.Utils;

public class DriverInfoControllers {
	private IDriverInfoController driverInfoController;
	public DriverInfoControllers(Context context) {
		driverInfoController = new DriverInfoManager(context);
	}
	public List<DriverInfo> getDriverInfoList(int type){
		return driverInfoController.getDriverInfoList(type);
	}
	public List<DriverInfo> queryDriverInfoByName(String name,int type){
		return driverInfoController.queryDriverInfoByName(name, type);
	}
	public DriverInfo queryDriverInfoById(int id){
		return driverInfoController.queryDriverInfoById(id);
	}
	public DriverInfo queryDriverInfoByCard(String card,int type){
		return driverInfoController.queryDriverInfoByCard(card, type);
	}

	
	/**
	 * 根据车牌号查询数据
	 * @param cardNumber
	 * @param type
	 * @return
	 */
	public List<DriverInfo> queryDriverInfoByCardNumber(String cardNumber, int type){
		return driverInfoController.queryDriverInfoByCardNumber(cardNumber, type);
	}
	
	/**
	 * 根据车牌号和姓名联合查询数据
	 * @param cardNumber
	 * @param type
	 * @return
	 */
	public List<DriverInfo> queryDriverInfoByCardNumberAndName(String name ,String cardNumber, int type){
		List<DriverInfo> infoList = new ArrayList<DriverInfo>();
		try {
			String params = "?name=" + name + "&carNumber=" + cardNumber;
			String url = "http://59.61.83.202:8089/queryCarMSG" + params;
			System.out.println("url:" + url);
			String data = Utils.getRemoteServicesData(url);
			System.out.println("data:" + data);
			if(!TextUtils.isEmpty(data)){
				JSONArray json = new JSONArray(data);
				for (int i = 0; i < json.length(); i++) {
					JSONObject obj = json.getJSONObject(i);
					DriverInfo info = new DriverInfo();
					info.setQueryID(obj.getString("Id"));
					info.setCardNumber(obj.getString("carNumber"));
					info.setDate(obj.getString("brandDate"));
					info.setName(obj.getString("name"));
					infoList.add(info);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return infoList;//driverInfoController.queryDriverInfoByCardNumberAndName(name, cardNumber, type);
	}
	
	public DriverInfo getDriverInfo(String id){
		DriverInfo info = new DriverInfo();
		String url = "http://59.61.83.202:8089/queryCarMSG?id=" + id;
		try {
			String data = Utils.getRemoteServicesData(url);
			if(!TextUtils.isEmpty(data)){
				JSONArray json = new JSONArray(data);
				for (int i = 0; i < json.length(); i++) {
					JSONObject obj = json.getJSONObject(i);
					info.setName(obj.getString("name"));
					info.setCardNumber(obj.getString("carNumber"));
					info.setDate(obj.getString("brandDate"));
					info.setAddress(obj.getString("model"));
					info.setResult(obj.getString("recognizeCode"));
					info.setCard(obj.getString("engineModel"));
					info.setNativeAddress(obj.getString("useNature"));
					info.setPhotoPath(obj.getString("photoPath"));
				}
			}
		} catch (Exception e) {
			return null;
		}
		return info;
	}
	
	//保存
	public String saveDriver(DriverInfo info){
		StringBuffer url = new StringBuffer("http://59.61.83.202:8089/addCarMSG");
		url.append("?name=").append(info.getName())
			.append("&carNumber=").append(info.getCardNumber())
			.append("&brandDate=").append(info.getDate())
			.append("&model=").append(info.getAddress())
			.append("&recognizeCode=").append(info.getResult())
			.append("&engineModel=").append(info.getCard())
			.append("&useNature=").append(info.getNativeAddress())
			.append("&photoPath=").append(info.getPhotoPath());
		System.out.println("url:" + url.toString());
		String result = "";
		try {
			String data = Utils.getRemoteServicesData(url.toString());
			System.out.println("data:" + data);
			JSONArray json = new JSONArray(data);
			for (int i = 0; i < json.length(); i++) {
				JSONObject obj = json.getJSONObject(i);
				result = obj.getString("result");
			}
		} catch (Exception e) {
			result = "";
			e.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * 根据身份证和姓名联合查询数据
	 * @param cardNumber
	 * @param type
	 * @return
	 */
	public DriverInfo queryDriverInfoByCardAndName(String name ,String card, int type){
		return driverInfoController.queryDriverInfoByCardAndName(name, card, type);
	}
	
	/**
	 * 插入一条新记录
	 * @param driverInfo
	 * @return
	 */
	public long insertDriverInfo(DriverInfo driverInfo, int type){
		return driverInfoController.insertDriverInfo(driverInfo, type);
	}
	
	public long updatePhotoPath(DriverInfo driverInfo, String path) {
		return driverInfoController.updatePhotoPath(driverInfo, path);
	}
}
